package com.atguigu.common.valid;

import lombok.SneakyThrows;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName ListValueConstraintValidator
 * @Description TODO
 * @Author 13290
 * @Date 2022/9/6 16:03
 *
 * public interface ConstraintValidator<A extends Annotation, T>
 *     第一个是需要校验的注解  第二个校验数据的类型 ConstraintValidator<ListValue,Integer>
 *
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set =  new HashSet<>();

    //初始化方法
    @Override
    public void initialize(ListValue constraintAnnotation) {
        //说明值的情况
        int[] vals = constraintAnnotation.vals();
        for (int val:vals
             ) {
            set.add(val);
        }
    }

    //判断是否校验成功
    /**
     * @author
     * @Description
     * @Date 16:15 2022/9/6
     * @param value 需要校验的值
     * @param constraintValidatorContext //校验的上下文环境信息
     * @Return boolean
     **/
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(value);
    }
}
